package  
{
	import org.flixel.*;
	import org.flixel.plugin.photonstorm.*;
	/**
	 * ...
	 * @author le-ray_y
	 */
	public class Enemy extends FlxExtendedSprite
	{	
		//************************************************************************
		//** 							VARIABLES								**
		//************************************************************************
		
		// Enemy Graphics
		[Embed(source = '../../Assets/enemy.png')]
		private static var _enemyGraph:Class;
		
		// Enemy Path
		private var _enemyPath:FlxPath = new FlxPath();
		
		// Anim Reset?
		private var _hasAnimReset:Boolean = false;
		
		// Level
		private var _level:Number = 0;
		
		//************************************************************************
		//** 							FUNCTIONS								**
		//************************************************************************
		
		public function Enemy(posX:Number, posY:Number, _health:Number, _speed:Number)
		{
			this.x = posX;
			this.y = posY;
			
			// Graphic
			this.loadGraphic(_enemyGraph, true, false, 43, 37);
			
			// Animations & Play
			this.addAnimation("idle", [0]);
			this.addAnimation("walk", [0, 1], 5);
			this.addAnimation("death", [2, 3, 4, 5], 5);
			this.addAnimation("attack", [6, 7, 8], 5);
			this.addAnimation("attackRabbit", [6, 7, 8], 5);
			this.play("walk");
			
			this.health = _health;
			this.pathSpeed = _speed;
			
			// Enemy Path
			var rand:Number = Math.random() * 25;
			_enemyPath.nodes = new Array(new FlxPoint(-30, 515 + rand), new FlxPoint(800, 515 + rand));
			this.followPath(_enemyPath, this.pathSpeed);
			
			this.exists = false;
		}
		
		override public function update():void
		{
			super.update();
			
			if (!this.alive)
			{
				this.play("death");
				if (_curFrame == 3 && _curAnim.name == "death")
				{
					kill();
				}
			}
			
			if (_curAnim.name == "attack")
			{
				if (_curFrame == 0)
				{
					_hasAnimReset = true;
				}
				if (_curFrame == 2 && _hasAnimReset)
				{
					Global.tower.damageTower(10 + 10 * _level);
					_hasAnimReset = false;
				}
				
			}
			
			if (_curAnim.name == "attackRabbit")
			{
				if (_curFrame == 0)
				{
					_hasAnimReset = true;
				}
				if (_curFrame == 2 && _hasAnimReset)
				{
					_hasAnimReset = false;
				}
				
			}
		}
		
		override public function reset(X:Number,Y:Number):void
		{
			this.x = X;
			this.y = Y;
			
			// Revive
			this.revive();
			
			// Anim
			this.play("walk");
			
			// Health & Speed
			this.health = 100;
			this.pathSpeed = 50;
			
			// Enemy Path
			var rand:Number = Math.random() * 25;
			_enemyPath.destroy();
			_enemyPath.nodes = new Array(new FlxPoint(-30, 515 + rand), new FlxPoint(800, 515 + rand));
			this.followPath(_enemyPath, this.pathSpeed);
		}
		
		override public function kill():void
		{
			super.kill();
		}
	}
}